<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="监测机构名称">
        <el-input v-model="queryParams.name" placeholder="请输入监测机构名称"></el-input>
      </el-form-item>

      <el-form-item label="联系人">
        <el-input v-model="queryParams.linkman" placeholder="请输入联系人"></el-input>
      </el-form-item>

      <el-form-item label="联系电话">
        <el-input v-model="queryParams.telephone" placeholder="请输入联系电话"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增按钮 -->
    <el-button type="primary" size="small" @click="handleAdd" style="margin-bottom: 15px;">+ 新增</el-button>

    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
      <el-table-column prop="name" label="监测机构名称" width="200"></el-table-column>
      <el-table-column prop="address" label="监测机构地址" width="200"></el-table-column>
      <el-table-column prop="content" label="监测内容"></el-table-column>
      <el-table-column prop="linkman" label="联系人" width="150"></el-table-column>
      <el-table-column prop="telephone" label="联系电话" width="180"></el-table-column>
      <el-table-column label="操作" width="120" fixed="right">
        <template #default="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 20px;"
    ></el-pagination>
    <!-- 新增对话框 -->
    <el-dialog title="添加监测机构" :visible.sync="dialogVisible" width="800px">
      <el-form :model="form" label-width="120px" :rules="rules" ref="form">
        <!-- 监测机构名称 -->
        <el-form-item label="监测机构名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入监测机构名称"></el-input>
        </el-form-item>

        <!-- 监测机构地址 -->
        <el-form-item label="监测机构地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入监测机构地址"></el-input>
        </el-form-item>

        <!-- 监测内容 -->
        <el-form-item label="监测内容" prop="content">
          <el-input type="textarea" v-model="form.content" placeholder="请输入监测内容" :autosize="{ minRows: 3, maxRows: 5 }"></el-input>
        </el-form-item>

        <!-- 联系人 -->
        <el-form-item label="联系人" prop="linkman">
          <el-input v-model="form.linkman" placeholder="请输入联系人"></el-input>
        </el-form-item>

        <!-- 联系电话 -->
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {listMonitoringorganization, addMonitoringorganization,editMonitoringorganization,removeMonitoringorganization} from './api.js'
export default {
  data() {
    return {
      queryParams: {
        name: '',
        linkman: '',
        telephone: ''
      },
      currentPage: 1,
      pageSize: 10,
      total: 1, // 总数据条数
      tableData: [],
      loading:false,
      dialogVisible: false, // 控制弹窗显示
      form: {
        name: '',
        address: '',
        content: '',
        linkman: '',
        telephone: '',
      },
      rules: {
        name: [{ required: true, message: '请输入监测机构名称', trigger: 'blur' }],
        address: [{ required: true, message: '请输入监测机构地址', trigger: 'blur' }],
        content: [{ required: true, message: '请输入监测内容', trigger: 'blur' }],
        linkman: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        telephone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList(){
      let params = {...this.queryParams, pageNum: this.currentPage, pageSize: this.pageSize};
      let res = await listMonitoringorganization(params);
      this.loading = true;
      if(res.code === 200) { 
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      }
    },
    handleSearch() {
      this.getList();
    },
    handleAdd() {
      this.dialogVisible = true;
      this.form = {
        name: '',
        address: '',
        content: '',
        linkman: '',
        telephone: '',
        image: ''
      };
    },
    handleEdit(row) {
      this.form = {...row};
      this.dialogVisible = true;
    },
       // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let apiFn = this.form.id ? editMonitoringorganization : addMonitoringorganization;
          apiFn(this.form).then(res => {
            this.$message.success('提交成功');
            this.dialogVisible = false;
            this.getList(); // 刷新列表
          });
        } else {
          console.log('校验失败');
          return false;
        }
      });
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeMonitoringorganization(id).then((res) => {
          this.getList();
          this.$message.success('删除成功');
        });
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      // 这里可以添加分页大小改变的逻辑
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      // 这里可以添加当前页改变的逻辑
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>